/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ro.futurefeed.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import ro.futurefeed.api.FeedTO;
import ro.futurefeed.model.Feed;
import ro.futurefeed.view.FeedView;

/**
 *
 * @author Alex
 */
public class FeedAssembler {
    
    public static SimpleDateFormat DATE_SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    public static final String SEPARATOR = ";";
    
    public static FeedTO toTO(Feed feed) {
        FeedTO feedTO = new FeedTO();
        feedTO.setContent(feed.getContent());
        
        try {
            feedTO.setDate(getDateWithTimezone(feed.getDateGMT(), feed.getTimezone()));        
        } catch(ParseException pe) {
            System.out.println(pe);
        }
        feedTO.setTimezone(feed.getTimezone());
        feedTO.setDestination(feed.getDestination());
        feedTO.setDestinationDescription(feed.getDestinationDescription());
        feedTO.setId(feed.getId());
        feedTO.setFbId(feed.getFbId());
        feedTO.setSendNotification(feed.getSendNotification());
        feedTO.setStatus(feed.getStatus());
        
        return feedTO;
    }
    
    public static FeedView toView(Feed feed) {
        FeedView feedView = new FeedView();
        feedView.setContent(feed.getContent());
        feedView.setId(feed.getId());
        feedView.setFbId(feed.getFbId());
        feedView.setSendNotification(feed.getSendNotification());
        feedView.setStatus(feed.getStatus());
        feedView.setTimezone(feed.getTimezone());
        
        try {
            feedView.setDate(getDateWithTimezoneAsDate(feed.getDateGMT(), feed.getTimezone()));        
        } catch(ParseException pe) {
            System.out.println(pe);
        }
        
        feedView.setMyWall(feed.getDestination() == null);
        
        if(!feedView.isMyWall()){
            feedView.setFriendId(feed.getDestination());
            if(feed.getDestinationDescription() != null){
                if(feed.getDestinationDescription().contains(SEPARATOR)){
                    String split[] = feed.getDestinationDescription().split(SEPARATOR);
                    feedView.setFriendName(split[0]);
                    feedView.setFriendPhotoURL(split[1]);
                }else{
                    feedView.setFriendName(feed.getDestinationDescription());
                    feedView.setFriendPhotoURL("n/a");
                }
            }
        }
        
        return feedView;
    }
    
    public static Feed toEntity(FeedTO feedTO) {
        Feed feed = new Feed();
        feed.setContent(feedTO.getContent());
        try {
            feed.setDateGMT(getDateWithoutTimezone(feedTO.getDate(), feedTO.getTimezone()));        
        } catch(ParseException pe) {
            System.out.println(pe);
        }
        feed.setTimezone(feedTO.getTimezone());
        feed.setDestination(feedTO.getDestination());
        feed.setDestinationDescription(feedTO.getDestinationDescription());
        feed.setId(feedTO.getId());
        feed.setFbId(feedTO.getFbId());
        feed.setSendNotification(feedTO.isSendNotification());
        feed.setStatus(feedTO.getStatus());
        
        return feed;
    }
    
    public static Feed toEntity(FeedView feedView) {
        Feed feed = new Feed();
        feed.setContent(feedView.getContent());
        feed.setStatus(feedView.getStatus());
        feed.setId(feedView.getId());
        feed.setFbId(feedView.getFbId());
        feed.setSendNotification(feedView.isSendNotification());
        feed.setTimezone(feedView.getTimezone());
        try {
            feed.setDateGMT(getDateWithoutTimezone(feedView.getDate(), feedView.getTimezone()));        
        } catch(ParseException pe) {
            System.out.println(pe);
        }
        if(!feedView.isMyWall()){
            feed.setDestination(feedView.getFriendId());
            feed.setDestinationDescription(feedView.getFriendName() + SEPARATOR + feedView.getFriendPhotoURL());
        }
        return feed;
    }

    private static String getDateWithTimezone(String dateGMT, Byte timezone) throws ParseException {
        Date parsedDate = DATE_SDF.parse(dateGMT);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(parsedDate);
        calendar.add(Calendar.HOUR, timezone);    
        return DATE_SDF.format(calendar.getTime());
    }

    private static String getDateWithoutTimezone(String date, Byte timezone) throws ParseException {
        Date parsedDate = DATE_SDF.parse(date);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(parsedDate);
        calendar.add(Calendar.HOUR, -timezone);
        return DATE_SDF.format(calendar.getTime());
    }
    
    private static String getDateWithoutTimezone(Date date, Byte timezone) throws ParseException {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.HOUR, -timezone);
        return DATE_SDF.format(calendar.getTime());
    }
    
    private static Date getDateWithTimezoneAsDate(String dateGMT, Byte timezone) throws ParseException {
        Date parsedDate = DATE_SDF.parse(dateGMT);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(parsedDate);
        calendar.add(Calendar.HOUR, timezone);    
        return calendar.getTime();
    }
}
